import { fetchCategoryTreeAPI } from "../../api/cateAPI";
import { Data } from "../../type/cateTypes";

// pages/cate/cate.ts
Page({
  /**
   * 页面的初始数据
   */
  data: <Data>{
    cateList: [],
    activeIndex: 0,
    loading: false,
  },
  /**
   * @description 获取商品分类的回调函数
   */
  async fetchCategoryTree() {
    try {
      this.setData({ loading: true })
      const { data: { data } } = await fetchCategoryTreeAPI()
      this.setData({ cateList: data, loading: false })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },
  /**
   * @description 更新激活索引的回调函数
   * 
   */
  changeActiveIndex(event: WechatMiniprogram.BaseEvent) {
    const { index } = event.currentTarget.dataset
    this.setData({ activeIndex: index })
  },
  /**
   * 
   * @description 跳转到商品列表的回调函数
   */
  goToDetail(event: WechatMiniprogram.BaseEvent) {
    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/subPackages/list/list?category2Id=${id}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.fetchCategoryTree()
  },
})